package com.example.utils;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一响应封装类
 * @param <T> 数据类型
 */
@Data
public class ReturnT<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    // 状态码常量
    public static final int CODE_SUCCESS = 200;
    public static final int CODE_ERROR = 500;

    private int code;
    private String msg;
    private T data;

    // 完整构造器
    public ReturnT(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    // 成功静态方法
    public static <T> ReturnT<T> success() {
        return new ReturnT<>(CODE_SUCCESS, "操作成功", null);
    }

    public static <T> ReturnT<T> success(T data) {
        return new ReturnT<>(CODE_SUCCESS, "操作成功", data);
    }

    // 失败静态方法
    public static <T> ReturnT<T> error(String msg) {
        return new ReturnT<>(CODE_ERROR, msg, null);
    }

    public static <T> ReturnT<T> error(int code, String msg) {
        return new ReturnT<>(code, msg, null);
    }
}

